/**
 * 
 */
package com.nh.actionPhone;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;

import com.nh.entity.BaseEntity;
import com.nh.entity.NgDictionary;
import com.nh.entity.NgUser;
import com.nh.service.IBaseService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：BasePhoneAction
 * 
 * 类描述： 手机客户端请求响应
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-4 上午12:20:40
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-4 上午12:20:40
 * 
 * 修改备注：
 * 
 * @version
 */
public class BasePhoneAction extends ActionSupport {
	private IBaseService baseService;
	private String phone;

	public IBaseService getBaseService() {
		return baseService;
	}

	/**
	 * 得到绝对路径（D:/tomcat/ng/）
	 * 
	 * @return
	 */
	public String getSavePath() {
		String serverPath = ServletActionContext.getServletContext().getRealPath(File.separator);
		serverPath = serverPath.replaceAll("\\\\", "/");
		return serverPath;
	}

	/**
	 * 得到当前工程路径URL(http://localhost:8080/ng)
	 * 
	 * @return
	 */
	public String getProjectURL() {
		ActionContext context = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
		return basePath;
		// return "http://192.168.1.101:8080";
	}

	public NgUser getCurrUser() {
		List<NgUser> list = this.getBaseService().findByProperty("phoneNum", phone, NgUser.class);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据key查找字典值
	 * 
	 * @param key
	 * @return
	 */
	public String getDictionaryValue(String key) {
		try {
			List<NgDictionary> list = this.getBaseService().findByProperty("dictionaryKey", key, NgDictionary.class);
			if (list.size() > 0) {
				return list.get(0).getDictionaryValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 添加字典
	 * 
	 * @param key
	 * @param value
	 */
	public void putDictionaryValue(String key, String value) {
		List<NgDictionary> list = this.getBaseService().findByProperty("dictionaryKey", key, NgDictionary.class);
		if (list.size() > 0) {
			NgDictionary dictionary = list.get(0);
			dictionary.setDictionaryValue(value);
			this.getBaseService().saveOrUpdate(dictionary, NgDictionary.class);
		} else {
			this.getBaseService().save(new NgDictionary(key, value));

		}
	}

	@Resource
	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}

	public Criteria createCriteria(Class<? extends BaseEntity> clazz) {
		return this.baseService.getBaseDao().getSession().createCriteria(clazz);
	}

	public boolean writResponse(String JSONString) {
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try {
			out = resp.getWriter();

			out.write(JSONString);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			if (out != null) {
				out.close();
			}
		}

		return true;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

}
